package tables

import (
	"github.com/GoAdminGroup/go-admin/context"
	"github.com/GoAdminGroup/go-admin/modules/db"
	"github.com/GoAdminGroup/go-admin/plugins/admin/modules/table"
	"github.com/GoAdminGroup/go-admin/template/types/form"
)

func GetCustomerTable(ctx *context.Context) table.Table {

	customer := table.NewDefaultTable(table.DefaultConfigWithDriver("mysql"))

	info := customer.GetInfo()
	info.SetFilterFormLayout(form.LayoutTwoCol)
	info.AddField("Id", "id", db.Int).
		FieldFilterable().
		FieldSortable()
	info.AddField("客户姓名", "name", db.Varchar).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("手机号", "phone", db.Varchar).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("地址", "address", db.Varchar).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("备注", "remark", db.Varchar).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("创建时间", "created_at", db.Timestamp).
		FieldSortable().
		FieldHide()
	info.AddField("更新时间", "updated_at", db.Timestamp).
		FieldSortable().
		FieldHide()

	info.SetTable("customer").SetTitle("客户管理").SetDescription("Customer")

	formList := customer.GetForm()
	formList.AddField("Id", "id", db.Int, form.Default).
		FieldDisableWhenCreate().
		FieldDisableWhenUpdate().
		FieldHide()
	formList.AddField("客户姓名", "name", db.Varchar, form.Text)
	formList.AddField("电话", "phone", db.Varchar, form.Text)
	formList.AddField("地址", "address", db.Varchar, form.Text)
	formList.AddField("备注", "remark", db.Varchar, form.Text)
	formList.AddField("创建时间", "created_at", db.Timestamp, form.Datetime).
		FieldDisableWhenUpdate().
		FieldHide().FieldNowWhenInsert()
	formList.AddField("修改时间", "updated_at", db.Timestamp, form.Datetime).
		FieldDisableWhenCreate().
		FieldHide().FieldNowWhenUpdate()

	formList.SetTable("customer").SetTitle("客户管理").SetDescription("Customer")

	return customer
}
